package svc

import (
	"go-weixin/app/music/musicapi/internal/config"
	"go-weixin/app/music/musicrpc/musicclient"
	"go-weixin/app/usercenter/rpc/userrpcclient"
	"go-weixin/common/apiserver"
	"go-weixin/common/rpcserver"

	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
	Config    config.Config
	MusicRpc  musicclient.Music
	UserRpc   userrpcclient.UserCenter
	ChenCache *redis.Redis
	ApiMDLE   *apiserver.Middleware
	RpcMDLE   *rpcserver.Middleware
}

func NewServiceContext(c config.Config) *ServiceContext {
	rpcMDLE := rpcserver.NewMiddleware()
	userRpc := userrpcclient.NewUserRpcClient(zrpc.MustNewClient(zrpc.RpcClientConf{Endpoints: c.UserRpc.Endpoints, NonBlock: c.UserRpc.NonBlock}, zrpc.WithUnaryClientInterceptor(rpcMDLE.ClientRpcMiddleware)))
	chenCache := redis.MustNewRedis(redis.RedisConf{
		Host: c.Cache.Host,
		Pass: c.Cache.Pass,
		Type: c.Cache.Type,
	})
	return &ServiceContext{
		Config:    c,
		UserRpc:   userRpc,
		ChenCache: chenCache,
		ApiMDLE:   apiserver.NewApiMiddleware(userRpc),
		RpcMDLE:   rpcMDLE,
		MusicRpc:  musicclient.NewMusic(zrpc.MustNewClient(zrpc.RpcClientConf{Endpoints: c.MusicRpcConf.Endpoints, NonBlock: c.MusicRpcConf.NonBlock}, zrpc.WithUnaryClientInterceptor(rpcMDLE.ClientRpcMiddleware))),
	}
}
